﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace DeploymentMaxLib
{
    public class ActionResult
    {
        public Guid Key { get; set; }
        public ActionResultSeverityType Severity { get; set; }
        public string Message { get; set; }
        public List<ActionResult> SubResultList { get; set; }

        public ActionResult()
        {
        }

        public ActionResult(string pMessage, ActionResultSeverityType pSeverity)
        {
            Key = Guid.NewGuid();
            Severity = pSeverity;
            Message = pMessage;
        }

        public void AddResult(Exception pException)
        {
            AddResult(pException.Message, ActionResultSeverityType.Critical);
        }

        public void AddResult(ActionResult pResult)
        {
            if (Key == null)
            {
                Key = pResult.Key;
                Severity = pResult.Severity;
                Message = pResult.Message;
            }
            else
            {
                if (SubResultList == null) SubResultList = new List<ActionResult>();
                SubResultList.Add(new ActionResult(pResult.Message, pResult.Severity));
            }
        }

        public void AddResult(string pMessage, ActionResultSeverityType pSeverity)
        {
            if (Key == null)
            {
                Key = Guid.NewGuid();
                Severity = pSeverity;
                Message = pMessage;
            }
            else
            {
                if (SubResultList == null) SubResultList = new List<ActionResult>();
                SubResultList.Add(new ActionResult(pMessage, pSeverity));
            }
        }

        public bool HasErrorOrCritical()
        {
            bool tHasErrorOrCritical = (Severity == ActionResultSeverityType.Error || Severity == ActionResultSeverityType.Critical);

            if(!tHasErrorOrCritical && SubResultList != null)
            {
                foreach(var tSubResult in SubResultList)
                {
                    tHasErrorOrCritical = tSubResult.HasErrorOrCritical();
                    if(tHasErrorOrCritical) break;
                }
            }

            return tHasErrorOrCritical;

        }
       

    }
}
